<!DOCTYPE html >
<html>
<head>
    <link rel="stylesheet" href="demos.css" type="text/css" media="screen" />
    
    <script src="../libraries/RGraph.common.core.js" ></script>
    <script src="../libraries/RGraph.common.effects.js" ></script>
    <script src="../libraries/RGraph.line.js" ></script>
    
    <title>A dynamically updating Line chart</title>
    
    <meta name="description" content="A dynamically updating Line chart. The data here is simply generated randomly but could just as easily be retrieved from a server via AJAX (for example)." />
</head>
<body>

    <h1>A dynamically updating Line chart</h1>
    
    <p>
        This chart updates itself 20 times a second.New values are appended to the data that's displayed and old values are removed
        from the data. Careful use is made of local variables (which are the fastest kind) - so that the updates are done smoothly.
    </p>

    <canvas id="cvs" width="1000" height="250">[No canvas support]</canvas>
    
    <script>

        window.onload = function (e)
        {
            var RG        = RGraph;
            var canvas    = document.getElementById("cvs");
            var obj       = null;
            var data      = [];
            var l         = 0; // The letter 'L' - NOT a one
            var numvalues = 1200;
        
            // Pre-pad the arrays with null values
            for (var i=0; i<numvalues; ++i) {
                data.push(null);
            }
        
            function drawGraph ()
            {
                RG.Clear(canvas);
                

                if (!obj) {
                    obj = new RG.Line('cvs', [])
                        .Set('xticks', 100)
                        .Set('background.barcolor1', 'white')
                        .Set('background.barcolor2', 'white')
                        .Set('title.xaxis', 'Last minute')
                        .Set('title.yaxis', 'Bandwidth (MB/s)')
                        .Set('title.vpos', 0.5)
                        .Set('title', 'Bandwidth used')
                        .Set('title.yaxis.pos', 0.5)
                        .Set('title.xaxis.pos', 0.5)
                        .Set('colors', ['black'])
                        .Set('linewidth',0.5)
                        .Set('yaxispos', 'right')
                        .Set('ymax', 50)
                        .Set('xticks', 25)
                        .Set('filled', true);
                    
                    var grad = obj.context.createLinearGradient(0,0,0,250);
                    grad.addColorStop(0, '#efefef');
                    grad.addColorStop(0.9, 'rgba(0,0,0,0)');
    
                    obj.Set('chart.fillstyle', [grad]);
                }

                // Add some data to the data arrays
                var len          = data.length;
                var lastvalue    = RG.is_null(data[len - 1]) ? 26 : data[len - 1];
                var random_value = RG.random(lastvalue  - 2,lastvalue  + 2);
               
               random_value = Math.max(random_value, 0);
               random_value = Math.min(random_value, 50);

                data.push(random_value);
                
                if (data.length > numvalues) {
                    data = RG.array_shift(data);
                }
    
                if (ISIE8) {
                    alert('[MSIE] Sorry, Internet Explorer 8 is not fast enough to support dynamic charts');
                } else {
                    obj.original_data[0] = data;
                    obj.Draw();
                    setTimeout(drawGraph, 50);
                }
            }
        
            drawGraph();
        }
    </script>

    <p>
        <a href="./">&laquo; Back</a>
    </p>

</body>
</html>